package lol.clann.minecraft.springboot.plugin.core.config.jackson.serializer;

import relocate.com.fasterxml.jackson.core.JsonGenerator;
import relocate.com.fasterxml.jackson.databind.JsonSerializer;
import relocate.com.fasterxml.jackson.databind.SerializerProvider;
import com.google.auto.service.AutoService;
import lol.clann.minecraft.springboot.api.spi.jackson.JacksonSerializer;
import relocate.ninja.leaping.configurate.ConfigurationNode;

import java.io.IOException;

/**
 *
 * @author pyz
 * @date 2019/5/3 3:05 PM
 */
@AutoService(JacksonSerializer.class)
public class ConfigurationNodeSerializer extends JsonSerializer<ConfigurationNode> implements JacksonSerializer{

    @Override
    public Class getType() {
        return ConfigurationNode.class;
    }

    @Override
    public void serialize(ConfigurationNode value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value.hasMapChildren()) {
            gen.writeObject(value.getChildrenMap());
        } else if (value.hasListChildren()) {
            gen.writeObject(value.getChildrenList());
        } else {
            gen.writeObject(value.getValue());
        }
    }
}
